/**
 * Project: arild.web
 * 
 * File Created at 2015年10月26日
 * $Id$
 * 
 * Copyright 2015 dface.cn Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * dface Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with www.dface.cn
 */
package cn.dface.ameba.web.interceptor;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import cn.dface.ameba.biz.vo.UserVo;
import cn.dface.ameba.toolkit.constant.GlobalConstants;
import cn.dface.ameba.toolkit.util.RequestUtils;
import cn.dface.ameba.toolkit.util.SessionUtils;
import cn.dface.framework.core.enums.ResultEnums;
import cn.dface.framework.core.vo.ResultVo;
import cn.dface.ucenter.biz.service.ServerService;

/**
 * DefaultUrlInterceptor
 * 
 * @author a1
 */
public class DefaultUrlInterceptor implements HandlerInterceptor {

    @Autowired
    ServerService ucenterService;
    
    @Value("${ucenter.loginUrl}")
    private String ucenterLoginUrl;
    
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler) throws Exception {
        
        String ticket = SessionUtils.getTicketFromRequest(request);
        if (null == ticket) {
            response.sendRedirect(ucenterLoginUrl);
            return false;
        } else {
            Object sessionUser = SessionUtils.getSessionValue(request, SessionUtils.USER);
            if (null == sessionUser) {
                ResultVo result = ucenterService.findUserInfo(ticket);
                if (result.getStatus() == ResultEnums.SUCESS.ordinal()) {
                    @SuppressWarnings("unchecked")
                    Map<String, Object> ucenterUser = (Map<String, Object>) result.getObject();
                    UserVo user = new UserVo();
                    user.setSid((Integer) ucenterUser.get("userSid"));
                    user.setName((String) ucenterUser.get("userName"));
                    SessionUtils.setSessionValue(request, SessionUtils.USER, user);
                } else {
                    response.sendRedirect(ucenterLoginUrl);
                    return false;
                }
            } 
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
        if (null == modelAndView) {
            return;
        }
        ModelMap modelMap = modelAndView.getModelMap();
        modelMap.put(GlobalConstants.CONTEXT_PATH, RequestUtils.findContextPath(request));
        
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
                                Object handler, Exception ex) throws Exception {
    }

 
}
